package com.bjpowernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.domain.Items;
import com.bjpowernode.mapper.ItemsMapper;
import com.bjpowernode.service.ItemsService;

@Service
@Slf4j
public class ItemsServiceImpl extends ServiceImpl<ItemsMapper, Items> implements ItemsService {
    @Resource
    private ItemsMapper itemsMapper;

    /**
     * 查询热门商品或推荐商品
     * type0 热门商品 买的比较多的
     * type1 推荐商品
     *
     * @param city 城市
     * @param type 区分是否被推荐
     * @return
     */
    @Override
    public List<Items> findHotProductOrRecommendation(String city, Integer type) {
        // 自定义设置查询sql的日志
        log.info("查询{}城市的{}商品", city, type == 0 ? "热门" : "推荐");
        // 当orderByDesc里面条件满足的时候查询,不满足就不查询
        List<Items> itemsList = itemsMapper.selectList(new LambdaQueryWrapper<Items>()
                .eq(Items::getCity, city) // 等同于 where city=#{city}
                .orderByDesc(type == 0, Items::getSales) // 等于 < if test = 'type=0'>
                .orderByDesc(type == 1, Items::getRecommendation) // 等于 < if test = 'type=0'>
                .last(" limit 5 ")
        );
        return itemsList;
    }
}
